package com.andy.qhb.heepay.common;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.andy.qhb.heepay.GatewayModel;

public class GatewayHelper {
	public static String SignMd5(String key,GatewayModel gatewayModel)
	{
		StringBuilder _StringSign = new StringBuilder();
       
        _StringSign.append("version=1")
            .append("&agent_id=" + gatewayModel.getagent_id())
            .append("&agent_bill_id=" + gatewayModel.getagent_bill_id())
            .append("&agent_bill_time=" + gatewayModel.getagent_bill_time())
            .append("&pay_type=" + gatewayModel.getpay_type());
       
        _StringSign.append("&pay_amt=" + gatewayModel.getpay_amt())
            .append("&notify_url=" + gatewayModel.getnotify_url())
            
            .append("&user_ip=" + gatewayModel.getuser_ip());
        
        _StringSign.append("&key=" + key);
        return Md5Tools.MD5(_StringSign.toString()).toLowerCase();
	}
	
	@SuppressWarnings("deprecation")
	public static String GatewaySubmitUrl(String sign,GatewayModel gatewayModel) throws UnsupportedEncodingException
	{
		StringBuilder sbURL = new StringBuilder();
		sbURL.append("https://pay.heepay.com/Phone/SDK/PayInit.aspx?");//��Ϊ���Ե�ַ���̻�Ӧʹ���ĵ��е���ʽ��ַ
		sbURL.append("version=" + gatewayModel.getversion());
        sbURL.append("&pay_type=" + gatewayModel.getpay_type());
        
        sbURL.append("&agent_id=" + gatewayModel.getagent_id());
        sbURL.append("&agent_bill_id=" + gatewayModel.getagent_bill_id());
        sbURL.append("&pay_amt=" + gatewayModel.getpay_amt());
        
        sbURL.append("&notify_url=" + gatewayModel.getnotify_url());
        sbURL.append("&return_url=" + gatewayModel.getreturn_url());
        sbURL.append("&user_ip=" + gatewayModel.getuser_ip());
        sbURL.append("&agent_bill_time=" + gatewayModel.getagent_bill_time());
        sbURL.append("&goods_name=" + URLEncoder.encode(gatewayModel.getgoods_name()));
        sbURL.append("&goods_num=" + gatewayModel.getgoods_num());
        sbURL.append("&remark=" + URLEncoder.encode(gatewayModel.getremark()));
        
        sbURL.append("&goods_note=" + URLEncoder.encode(gatewayModel.getgoods_note()));
        sbURL.append("&sign=" + sign);
		return sbURL.toString();
	}
	
	//�㸶����ѯ����ǩ��
	public static String SignQueryMd5(String key,GatewayModel gatewayModel)
	{
		StringBuilder _StringSign = new StringBuilder();
		
		_StringSign.append("version=" + gatewayModel.getversion())
        .append("&agent_id=" + gatewayModel.getagent_id())
        .append("&agent_bill_id=" + gatewayModel.getagent_bill_id())
        
        
		.append("&key=" + key);
		return Md5Tools.MD5(_StringSign.toString()).toLowerCase();
	}
	
	public static String GatewaySubmitQuery(String sign,GatewayModel gatewayModel) throws UnsupportedEncodingException
	{
		StringBuilder sbURL = new StringBuilder();
		sbURL.append("version=1");
        sbURL.append("&agent_id=" + gatewayModel.getagent_id());
        sbURL.append("&agent_bill_id=" + gatewayModel.getagent_bill_id());
       
        sbURL.append("&sign=" + sign);
        
		return sbURL.toString();
	}
	
	
	public static boolean VerifiSign(String urlStr)
	{
		String[] str = urlStr.split("sign=");
		String[] str1=str[0].split("bill_info>");
		String[] str2=str[1].split("</bill_info");
		String mySign = Md5Tools.MD5(str1[1] + "key="+DataHelper.sign_key).toLowerCase();
		System.out.println(str[0]+"-------"+str2[0]+"======="+mySign);
		
		if(mySign.equals(str2[0]))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
